//
//  SJPAppenedTableController.m
//  SWS_Project00
//
//  Created by 马继鵬 on 17/3/9.
//  Copyright © 2017年 SWS. All rights reserved.
//

#import "SJPAppenedTableController.h"
#import "SJPAppendTableViewCell.h"
#import "SJPAppendModel.h"

@interface SJPAppenedTableController ()
@property (strong,nonatomic) NSArray *appendList;
@property (strong,nonatomic)NSMutableArray<SJPAppendModel*>* selectList;
@end
static NSString* cell_ID = @"cell_ID";
@implementation SJPAppenedTableController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor redColor];
    [self loadData];
    [self.tableView registerClass:[SJPAppendTableViewCell class] forCellReuseIdentifier:cell_ID];
    UIBarButtonItem* bar = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(clickToBack:)];
    
    self.navigationItem.rightBarButtonItem = bar;
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor blackColor]];
    
}
#pragma
#pragma-添加点击方法
- (void)clickToBack:(id)sender
{
    NSMutableString* strM = [NSMutableString new];
    //获取数据
    for(SJPAppendModel* model in _selectList)
    {
        [strM appendString:[NSString stringWithFormat:@"%@/",model.title]];
    }
    if (self.titleBlock)
    {
        self.titleBlock(strM.copy);
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma
#pragma-数据加载
- (void)loadData
{
    NSString* path = [[NSBundle mainBundle]pathForResource:@"appendList.plist" ofType:nil];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    NSMutableArray* arrayM = [NSMutableArray array];
    
    for (NSString* str in array)
    {
        SJPAppendModel* model = [SJPAppendModel new];
        model.title = str;
        model.selectedClick = NO;
        [arrayM addObject:model];
    }
    _appendList = arrayM.copy;
    _selectList = [NSMutableArray array];
}
#pragma
#pragma-数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _appendList.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SJPAppendTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cell_ID forIndexPath:indexPath];
    cell.model = _appendList[indexPath.row];
    cell.backBlock = ^(BOOL selected){
        SJPAppendModel* model = _appendList[indexPath.row];
        model.selectedClick = selected;
        if (selected == YES)
        {
            [_selectList addObject:model];
        }else
        {
            [_selectList removeObject:model];
        }
    };
    return cell;
}

#pragma
#pragma-代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//添加组头
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"注意点击CELL不做事情,点击cell改变文字";
}
@end
